<template>
    <view>
        <!-- pages/setCoupon/setCoupon.wxml -->
        <view class="tabs">
            <van-dropdown-menu :active-color="mainColor">
                <van-dropdown-item :value="storeId" :options="stores" @change="storeDropdown" />
                <van-dropdown-item :value="type" :options="types" @change="typeDropdown" />
            </van-dropdown-menu>
        </view>
        <view class="container" :style="isSelect ? 'padding-bottom:0;' : isIpx ? 'padding-bottom:188rpx' : 'padding-bottom:120rpx'">
            <block v-if="list.length">
                <view class="list">
                    <view class="item" v-for="(item, index) in list" :key="index">
                        <block v-if="item.type === 1">
                            <image class="img" src="/static/img/coupon_blue.png" mode="scaleToFill"></image>
                        </block>

                        <block v-if="item.type === 2">
                            <image class="img" src="/static/img/coupon_yellow.png" mode="scaleToFill"></image>
                        </block>

                        <block v-if="item.type === 3">
                            <image class="img" src="/static/img/order_blue.png" mode="scaleToFill"></image>
                        </block>

                        <view class="info">
                            <view class="left">
                                <view class="name">{{ item.couponName }}</view>
                                <view class="text">适用门店：{{ item.storeName }}</view>
                                <view class="text">
                                    包间限制：{{
                                        item.roomType === 1
                                            ? '小包'
                                            : item.roomType === 2
                                            ? '中包'
                                            : item.roomType === 3
                                            ? '大包'
                                            : item.roomType === 4
                                            ? '豪包'
                                            : item.roomType === 5
                                            ? '商务包'
                                            : '不限制'
                                    }}
                                </view>
                                <view class="text">
                                    抵扣额度：
                                    <text v-if="item.type === 2">{{ item.price }}元</text>
                                    <text v-else>{{ item.price }}小时</text>
                                </view>
                                <view class="text">
                                    使用门槛：
                                    <text v-if="item.type === 2">满{{ item.minUsePrice }}元可用</text>
                                    <text v-else>满{{ item.minUsePrice }}小时可用</text>
                                </view>
                                <view class="text">过期时间：{{ item.expriceTime }}</view>
                                <view class="text">
                                    状态：
                                    <text :class="item.status === 0 ? 'normal' : 'expired'">{{ item.status === 0 ? '待使用' : item.status === 1 ? '已使用' : '已过期' }}</text>
                                </view>
                            </view>
                            <view class="right">
                                <view class="btns" v-if="isSelect">
                                    <button hover-class="button-click" class="btn bg-primary" @tap="select" :data-info="item.couponId">选择</button>
                                </view>
                                <!-- 优惠券管理 -->
                                <view class="btns" v-else>
                                    <navigator class="btn primary" :url="'../setVip/setVip?isSelect=1&&couponId=' + item.couponId">赠送用户</navigator>
                                    <navigator class="btn yellow" :url="'../setCouponInfo/setCouponInfo?couponId=' + item.couponId">修改设置</navigator>
                                </view>
                            </view>
                        </view>
                    </view>
                </view>
            </block>
            <block v-else>
                <view class="nodata">暂无数据</view>
            </block>
        </view>
        <!-- 底部按钮 -->
        <view class="bt">
            <navigator v-if="!isSelect" url="../setCouponInfo/setCouponInfo" :class="'bottom bg-primary ' + (isIpx ? 'fix-iphonex-button transparent' : '')">添加优惠券</navigator>
        </view>
    </view>
</template>

<script>
// pages/setCoupon/setCoupon.js
const app = getApp();
var http = require('../../../utils/http');
var util1 = require('../../../utils/util.js');
export default {
    data() {
        return {
            storeId: '',
            //列表搜索门店id
            stores: [],
            types: [
                {
                    text: '全部类型',
                    value: ''
                },
                {
                    text: '抵扣券',
                    value: 1
                },
                {
                    text: '满减券',
                    value: 2
                }
            ],
            type: '',
            isSelect: 0,
            pageNo: 1,
            pageSize: 10,
            canLoadMore: true,
            //是否还能加载更多
            list: [],
            userId: '',
            //给用户id赠送优惠券
            isIpx: app.globalData.isIpx ? true : false,
            mainColor: app.globalData.mainColor
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad(options) {
        let isSelect = options.isSelect ? parseInt(options.isSelect) : '';
        let userId = options.userId ? parseInt(options.userId) : '';
        this.setData({
            isSelect: isSelect,
            userId: userId
        });
        if (isSelect === 1) {
            uni.setNavigationBarTitle({
                title: '赠送优惠券'
            });
        }
        this.getXiaLaListAdmin();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getMainListdata('refresh');
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        let that = this;
        this.setData({
            pageNo: 1,
            canLoadMore: true,
            list: []
        });
        this.getMainListdata('refresh');
        uni.stopPullDownRefresh();
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        let that = this;
        if (that.canLoadMore) {
            that.pageNo++;
            this.getMainListdata('');
        } else {
            uni.showToast({
                title: '我是有底线的...'
            });
        }
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {},
    methods: {
        //管理员获取门店下拉列表数据
        getXiaLaListAdmin: function (e) {
            var that = this;
            //if (app.globalData.isLogin)
            {
                http.request(
                    '/member/store/getStoreList',
                    '1',
                    'get',
                    {},
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        console.info('下拉门店数据===');
                        console.info(info);
                        if (info.code == 0) {
                            let stores = [];
                            info.data.map((it) => {
                                stores.push({
                                    text: it.key,
                                    value: it.value
                                });
                            });
                            stores.unshift({
                                text: '全部门店',
                                value: ''
                            });
                            that.setData({
                                stores: stores,
                                storeId: stores[0].value
                            });
                        } else {
                            uni.showModal({
                                content: '请求服务异常，请稍后重试',
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
        },

        //门店下拉菜单发生变化
        storeDropdown(event) {
            //console.log(event)
            this.stores.map((it) => {
                if (it.value == event.detail) {
                    this.setData({
                        storeId: it.value
                    });
                }
            });
            this.getMainListdata('refresh');
        },

        //类型下拉菜单发生变化
        typeDropdown(event) {
            this.setData({
                type: event.detail
            });
            this.getMainListdata('refresh');
        },

        //获取列表数据
        getMainListdata: function (e) {
            var that = this;
            let message = '';
            if (app.globalData.isLogin) {
                if (e == 'refresh') {
                    //刷新，page变为1
                    message = '正在加载';
                    that.setData({
                        pageNo: 1
                    });
                }
                http.request(
                    '/member/manager/getCouponPage',
                    '1',
                    'post',
                    {
                        pageNo: that.pageNo,
                        pageSize: that.pageSize,
                        storeId: that.storeId,
                        type: that.type,
                        roomId: '',
                        orderHour: ''
                    },
                    app.globalData.userDatatoken.accessToken,
                    message,
                    function success(info) {
                        console.info('返回111===');
                        console.info(info);
                        if (info.code == 0) {
                            if (e == 'refresh') {
                                that.setData({
                                    list: info.data.list
                                });
                                if (info.data.list.length === 0) {
                                    that.setData({
                                        canLoadMore: false
                                    });
                                }
                            } else {
                                if (info.data != null && info.data.list.length <= info.data.total) {
                                    that.setData({
                                        canLoadMore: false
                                    });
                                }
                                let arr = that.list;
                                let arrs = arr.concat(info.data.list);
                                that.setData({
                                    list: arrs
                                });
                            }
                        } else {
                            uni.showModal({
                                content: '请求服务异常，请稍后重试',
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
        },

        // 选择优惠券
        select: function (e) {
            var that = this;
            let couponId = e.currentTarget.dataset.info;
            console.log('userId' + that.userId);
            uni.showModal({
                title: '提示',
                content: '是否确定赠送该优惠券',
                complete: (res) => {
                    if (res.cancel) {
                    }
                    if (res.confirm) {
                        var that = this;
                        if (app.globalData.isLogin) {
                            http.request(
                                '/member/manager/giftCoupon',
                                '1',
                                'post',
                                {
                                    couponId: couponId,
                                    userId: that.userId
                                },
                                app.globalData.userDatatoken.accessToken,
                                '',
                                function success(info) {
                                    console.info('返回111===');
                                    console.info(info);
                                    if (info.code == 0) {
                                        uni.showToast({
                                            title: '赠送成功'
                                        });
                                        uni.navigateBack();
                                    } else {
                                        uni.showModal({
                                            content: '请求服务异常，请稍后重试',
                                            showCancel: false
                                        });
                                    }
                                },
                                function fail(info) {}
                            );
                        }
                    }
                }
            });
        }
    }
};
</script>
<style>
@import './setCoupon.css';
</style>
